﻿var queryStrings = function() {//get url querystring
    var params=document.location.search,reg=/(?:^\?|&)(.*?)=(.*?)(?=&|$)/g,temp,args={};
    while((temp=reg.exec(params))!=null) args[temp[1]]=decodeURIComponent(temp[2]);
    return args;
};
var queryString = function(key){
    return (document.location.search.match(new RegExp("(?:^\\?|&)"+key+"=(.*?)(?=&|$)"))||['',null])[1];
}
function redirectUrl(url) {
    window.location.href = url;
}
function showprepic(id, file) {
    document.all.id.src = "file://" + file;
}
function getPath(obj) {
    if (obj) {
        if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
            obj.select();
            return document.selection.createRange().text;
        }
        else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            if (obj.files) {
                return obj.files.item(0).getAsDataURL();
            } return obj.value;
        } 
        return obj.value;
    }
}
function preview(title, num) {
    obj = obj_getbyid(title);

    var p_obj = obj_getbyid("p_" + obj.id);
    if (obj.value == "") {
        p_obj.innerHTML = obj.value;
        obj.focus();
        return false;
    }
    var t_num = obj.value.replace(/[^\x00-\xff]/g, "00").length;
    if (t_num > num) {
        obj.maxLength = obj.value.length;
        obj.focus();
        if (obj.id == "description_1") obj_getbyid("description_2").focus();
        return false;
    }
    else {
        obj.maxLength = num;
        p_obj.innerHTML = obj.value;
        if (t_num == num) {
            if (obj.id == "description_1") obj_getbyid("description_2").focus();
        }
        return true;
    }
}
function obj_getbyid(id) {
    itm = null;
    if (document.getElementById) {
        itm = document.getElementById(id);
    } else if (document.all) {
        itm = document.all[id];
    } else if (document.layers) {
        itm = document.layers[id];
    }
    return itm;
}
var errcode = queryString("retcode");
var url = queryString("url");
if (errcode != null) {
    if (errcode == 1) { alert("网店推广增加成功!"); window.location.replace("ShopList.aspx?action=gopage&pageIndex=1"); }
    if (errcode <= 0) {
        if (errcode == -1 || errcode == 0) { alert("网店推广增加失败!"); }
        window.location.replace(url);
    }
}
function initData(shop) {
    try {
        if (shop != null) {
            if (document.all("SearchEngineType") != null) {
                document.all("SearchEngineType").value = shop.SearchEngineType;
            }
            if (document.all("AutoID") != null) {
                document.all("AutoID").value = shop.AutoID;
            }
            if (document.all("CommitCount") != null){ document.all("CommitCount").value = shop.CommitCount;
            }
            if (document.all("Url") != null) {
                document.all("Url").value = shop.Url;
            }
            if (document.all("ShopName") != null) {
                document.all("ShopName").value = shop.ShopName;
            }
            if (document.all("Shipping") != null) {
                document.all("Shipping").value = shop.Shipping;
            }
            if (document.all("Region") != null) {
                document.all("Region").value = shop.Region;
            }
            if (document.all("QQ") != null) {
                document.all("QQ").value = shop.QQ;
            }
            if (document.all("ProductPrice") != null) {
                document.all("ProductPrice").value = shop.ProductPrice;
            }
            if (document.all("ProductPicture") != null) {
                document.all("ProductPicture").value = shop.ProductPicture;
            }
            if (document.all("ProductName") != null) {
                document.all("ProductName").value = shop.ProductName;
            }
            if (document.all("KeyWords") != null) {
                document.all("KeyWords").value = shop.KeyWords;
            }
            if (document.all("IsAllDayShipping") != null) {
                document.all("IsAllDayShipping").checked = shop.IsAllDayShipping;
            }
            if (document.all("IsCashonDelivery") != null) {
                document.all("IsCashonDelivery").checked = shop.IsCashonDelivery;
            }
            if (document.all("IsCreditCard") != null) {
                document.all("IsCreditCard").checked = shop.IsCreditCard;
            }
            if (document.all("IsGenuineProtection") != null) {
                document.all("IsGenuineProtection").checked = shop.IsGenuineProtection;
            }
            if (document.all("IsLogo") != null) {
                document.all("IsLogo").checked = shop.IsLogo;
            }
            if (document.all("IsProtection") != null) {
                document.all("IsProtection").checked = shop.IsProtection;
            }
            if (document.all("IsRetreat") != null) {
                document.all("IsRetreat").checked = shop.IsRetreat;
            }
            if (document.all("IsPublicbaby") != null) {
                document.all("IsPublicbaby").checked = shop.IsPublicbaby;
                //document.all("p_IsPublicbaby").checked = shop.IsPublicbaby;
            }
        }
     } catch (ex) { }
}

$(document).ready(function () {
    try {
        if (user != null) {
            if (document.all("userName") != null) document.all("userName").innerText = user.username;
        }
    } catch (ex) { }
    var url = "RequestHandler.ashx" + document.location.search + "&st=" + Math.random();
    $.get(url, function (data) {
        var json = (new Function('return ' + data))();
        //        alert(json);
        initData(json);
    });
});